package com.haofengsoft.scce.controller.consultation;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haofengsoft.scce.adapter.information.FocusNewModel;
import com.haofengsoft.scce.db.biz.consultation.FocusNewBiz;
import com.haofengsoft.scce.db.entity.consultation.FocusNew;

/**
 * Created by admin on 2015/7/14.
 */
public class FocusNewController {
    private static FocusNewController instance = null;
    private Context appContext;

    public static synchronized FocusNewController getInstance(Context context) {
        if (instance == null) {
            instance = new FocusNewController(context);
        }
        return instance;
    }

    public FocusNewController(Context appContext) {
        this.appContext = appContext;
    }

    // public List<AdvertModel> gainAdvertList() {
    // List<AdvertInfo> infoList =
    // AdvertInfoBiz.getInstance(appContext).gainAdvertList();
    // List<AdvertModel> modelList = new ArrayList<AdvertModel>();
    // for (AdvertInfo info : infoList) {
    // AdvertModel model = new AdvertModel();
    // model.setImageUr(info.getImageUrl());
    // modelList.add(model);
    // }
    // return modelList;
    // }
    //处理要闻 hasLoadedOnce 是否已被加载过一次，第二次就不再去请求数据了
    public List<FocusNewModel> handleFocusNewList(String json) {
        try {
            List<FocusNewModel> modelList = new ArrayList<FocusNewModel>();
            List<FocusNew> focusNewModelList = new ArrayList<FocusNew>();
            JSONArray newsModelArray = JSON.parseObject(json).getJSONArray("NewList");
            for (int i = 0; i < newsModelArray.size(); i++) {
                JSONObject newsModelObj = newsModelArray.getJSONObject(i);
                FocusNew focusNewModel = new FocusNew();
                focusNewModel.setTitle(newsModelObj.getString("Title"));
                focusNewModel.setSummary(newsModelObj.getString("Summary"));
                focusNewModel.setStrCreateDate(newsModelObj.getString("StrCreateDate"));
                focusNewModel.setId(newsModelObj.getString("ID"));
                focusNewModelList.add(focusNewModel);
            }
            //首次加载，清楚掉原来的数据
            if (focusNewModelList.size() > 1) {
                FocusNewBiz.getInstance(appContext).deleteAll();
                //添加数据库，第一次，和下拉的
                FocusNewBiz.getInstance(appContext).insertFocusNewList(focusNewModelList);
                for (FocusNew focus : focusNewModelList) {
                    FocusNewModel model = new FocusNewModel();
                    model.setId(focus.getId());
                    model.setTime(focus.getStrCreateDate());
                    model.setDetail(focus.getSummary());
                    model.setTitle(focus.getTitle());
                    modelList.add(model);
                }
            } else {
                modelList = null;
            }
            return modelList;

        } catch (Exception e) {
            return null;
        }
    }

    public List<FocusNewModel> gainFocusNewModels() {
        List<FocusNew> focusNewList = FocusNewBiz.getInstance(appContext).gainFocusNewList();
        List<FocusNewModel> focusNewModelList = new ArrayList<FocusNewModel>();
        for (FocusNew focus : focusNewList) {
            FocusNewModel model = new FocusNewModel();
            model.setId(focus.getId());
            model.setTime(focus.getStrCreateDate());
            model.setDetail(focus.getSummary());
            model.setTitle(focus.getTitle());
            focusNewModelList.add(model);
        }
        return focusNewModelList;
    }

    //处理要闻
    public boolean handleFocusNew(String json) {
        try {
            List<FocusNew> focusNewModelList = new ArrayList<FocusNew>();
            JSONArray newsModelArray = JSON.parseObject(json).getJSONArray("NewList");
            for (int i = 0; i < newsModelArray.size(); i++) {
                JSONObject newsModelObj = newsModelArray.getJSONObject(i);
                FocusNew focusNewModel = new FocusNew();
                focusNewModel.setTitle(newsModelObj.getString("Title"));
                focusNewModel.setSummary(newsModelObj.getString("Summary"));
                focusNewModel.setStrCreateDate(newsModelObj.getString("StrCreateDate"));
                focusNewModel.setId(newsModelObj.getString("ID"));
                focusNewModelList.add(focusNewModel);
            }
            FocusNewBiz.getInstance(appContext).insertFocusNewList(focusNewModelList);
            return true;

        } catch (Exception e) {
            return false;
        }
    }
    //清楚老数据


}
